package com.mindbook.vip.gawxchatinfo.config.shiro;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.pam.ModularRealmAuthenticator;
import org.apache.shiro.realm.Realm;

import java.util.Collection;

/**
 * @Author wxb
 * @Datatime 2022/4/6 15:30
 * @Descript 自定义处理多realm 认证访问多个realm的问题
 */
public class CustomModularRealmAuthenticator extends ModularRealmAuthenticator {

    @Override
    protected AuthenticationInfo doAuthenticate(AuthenticationToken authenticationToken) throws AuthenticationException {
        //1.从token中获取当前的realm
        LoginTypeToken token = (LoginTypeToken) authenticationToken;
        String realmName = token.getLoginType();
        //2.获取配置中的Realm
        this.assertRealmsConfigured();
        Realm realm = null;
        Collection<Realm> realms = this.getRealms();
        for (Realm r: realms){
            if(r.getClass().getSimpleName().equals(realmName)){
                realm = r;
                break;
            }
        }
        return this.doSingleRealmAuthentication(realm, authenticationToken);
    }

}
